const deepClone = require("../template.js")

/** 3. 循环引用测试 */
function testCircular() {
  console.log(
    "3.1 循环引用:",
    (() => {
      const obj = { a: 1 }
      obj.self = obj
      const cloned = deepClone(obj)
      return cloned.a === 1 && cloned.self === cloned && cloned !== obj
    })()
  )

  console.log(
    "3.2 复杂循环引用:",
    (() => {
      const obj1 = { name: "obj1" }
      const obj2 = { name: "obj2" }
      obj1.ref = obj2
      obj2.ref = obj1
      const root = { obj1, obj2 }
      const cloned = deepClone(root)
      return (
        cloned.obj1.ref.name === "obj2" &&
        cloned.obj2.ref.name === "obj1" &&
        cloned.obj1.ref === cloned.obj2 &&
        cloned !== root
      )
    })()
  )

  console.log(
    "3.3 数组中的循环引用:",
    (() => {
      const arr = [1, 2]
      arr.push(arr)
      const cloned = deepClone(arr)
      return (
        cloned[0] === 1 &&
        cloned[1] === 2 &&
        cloned[2] === cloned &&
        cloned !== arr
      )
    })()
  )

  console.log("--------------------")
}

testCircular()

module.exports = testCircular
